package com.couldsail.takeaway.controller.user;

import com.couldsail.takeaway.common.result.PageResult;
import com.couldsail.takeaway.common.result.Result;
import com.couldsail.takeaway.pojo.dto.OrderCancelDTO;
import com.couldsail.takeaway.pojo.dto.OrdersDTO;
import com.couldsail.takeaway.pojo.vo.OrderVO;
import com.couldsail.takeaway.service.OrdersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "订单管理", description = "订单相关接口")
@RestController("UserOrderController")
@RequestMapping("/user/order")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class OrdersController {

    private final OrdersService orderService;

    @Operation(summary = "用户下单")
    @ApiResponse(responseCode = "200", description = "下单成功")
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody OrdersDTO ordersDTO) {
        log.info("用户下单：{}", ordersDTO);
        orderService.submitOrder(ordersDTO);
        return Result.success("下单成功");
    }

    @Operation(summary = "取消订单")
    @PostMapping("/cancel")
    public Result<String> cancelOrder(@RequestBody OrderCancelDTO orderCancelDTO) {
        log.info("取消订单{}",orderCancelDTO);
        orderService.cancelOrder(orderCancelDTO);
        return Result.success("订单取消成功");
    }

    @Operation(summary = "查询订单列表")
    @GetMapping("/list")
    public Result<PageResult<OrderVO>> list(
            @Parameter(description = "订单状态") @RequestParam(required = false)
            String status,
            @Parameter(description = "当前页") @RequestParam(required = true)
            long current,
            @Parameter(description = "每页显示条数，默认十") @RequestParam(required = false)
            Integer size
    ) {
        log.info("订单列表查询status:{}current:{} size:{}",status,current,size);
        PageResult<OrderVO> list = orderService.getOrderList(status,current,size);
        return Result.success(list);
    }

    @Operation(summary = "查询订单详情")
    @GetMapping("/detail/{id}")
    public Result<OrderVO> getById(@PathVariable("id") String id) {
        log.info("查询订单详情{}",id);
        OrderVO orderVO = orderService.getOrderById(id);
        return Result.success(orderVO);
    }

    @Operation(summary = "再来一单")
    @PostMapping("/again/{id}")
    public Result<String> again(@PathVariable String id) {
        log.info("再来一单{}",id);
        orderService.again(id);
        return Result.success("操作成功");
    }
}